package com.imstlife.vending.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imstlife.vending.R;
import com.imstlife.vending.listener.MyDrinkListListener;
import com.imstlife.vending.utils.Cof;
import com.imstlife.vending.utils.DrinkCountUtil;
import com.imstlife.vending.utils.GlideUtil;
import com.imstlife.vending.vo.DrinkInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lihaifeng on 17/2/23.
 */

public class MyDrinkListView  extends LinearLayout{

    private List<DrinkInfo> drinkInfos;
    private String from;

    int [] drinklayout = new int[]{};
    private Context context;


    private View rootView;

    private Map<String,ViewHolder> myCache;


    private String TAG = "MyDrinkListView";

    private LinearLayout drinkParentLayout;

    private int width;
    MyDrinkListListener listListener;

    public MyDrinkListView(Context context) {
        super(context);
        this.context = context;

        myCache = new HashMap<>();
    }

    public MyDrinkListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        myCache = new HashMap<>();
    }

    public MyDrinkListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        myCache = new HashMap<>();
    }

    /**
     * @param drinkInfos 从服务器返回的商品列表
     * @param from  中吉版 or  以勒版
     */
    public void configView(List<DrinkInfo> drinkInfos,String from,int width,MyDrinkListListener  listListener ){
        this.drinkInfos = drinkInfos;
        this.from = from;
        this.width = width;
        this.listListener = listListener;
        Log.i(TAG,"厂家："+from);

        if(Cof.MODEL_01.equals(from)){
            drinklayout = new int[]{5,10,10,10,10};
        }else if (Cof.MODEL_02.equals(from)){
            drinklayout = new int[]{6,6,6,4,6};
        }else if (Cof.MODEL_03.equals(from)){
            drinklayout =  new int[]{10,10,10,10,10,10};
        }else if (Cof.MODEL_04.equals(from)){
            drinklayout =  new int[]{6,6,6,6,6,6};
        }

        initView();

    }

    int index = 0 ;
    private void initView(){


        this.setOrientation(LinearLayout.VERTICAL);
         LayoutParams params = (LayoutParams) this.getLayoutParams();

        params.height=LayoutParams.MATCH_PARENT;
        params.width = LayoutParams.MATCH_PARENT;
        this.setLayoutParams(params);
        //1.循环行
        for(int i = 0 ; i< drinklayout.length;i++){//行
//            LinearLayout  r = new LinearLayout(context);
            //创建行父布局
            RelativeLayout r = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.drink_row_layout,null);
            LayoutParams p1 = new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
            p1.weight=1;
            p1.width = width;
            p1.height = 0;
            r.setLayoutParams(p1);
            Log.i(TAG,r.getLayoutParams().width+"");
            LinearLayout ll = (LinearLayout) r.findViewById(R.id.ll_drink_row_layout_ll);
            //2.循环行内的列
            for(int j = 0 ; j < drinklayout[i];j++){//列
                if(index==drinkInfos.size()){
                    break;
                }
//                Log.i(TAG,"dinkinfo: "+drinkInfos.get(index));

                RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.drink_item_layout,null);
                LayoutParams p = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
                p.weight=1;
                p.width = 0;
                p.height = RelativeLayout.LayoutParams.MATCH_PARENT;
                itemLayout.setLayoutParams(p);
                ViewHolder holder = myCache.get(drinkInfos.get(index).getNum()+"");
                if(holder==null){
                    holder = new ViewHolder();
                    holder.ivDrinkPic = (ImageView) itemLayout.findViewById(R.id.img_item_drink_icon);
                    holder.ivDrinkOver = (ImageView) itemLayout.findViewById(R.id.img_item_drink_over);
                    holder.tvPrice = (TextView) itemLayout.findViewById(R.id.text_item_drink_price);
                    myCache.put(drinkInfos.get(index).getNum()+"",holder);
                }else{
                    holder = myCache.get(drinkInfos.get(index).getNum()+"");
                }
                final DrinkInfo drinkInfo = drinkInfos.get(index);

                int count =  DrinkCountUtil.getDrinkCountByNum(context,drinkInfos.get(index).getNum()+"");
                holder.count = count;
                holder.tvPrice.setText(drinkInfos.get(index).getPrice()+"");
                if(count>0){
                    holder.ivDrinkOver.setVisibility(View.INVISIBLE);
                    holder.ivDrinkPic.setVisibility(View.VISIBLE);
                    GlideUtil.dontAnimLoad(context,drinkInfos.get(index).getMinPicUrl(),holder.ivDrinkPic);
                }else{
                    holder.ivDrinkOver.setVisibility(View.VISIBLE);
                    holder.ivDrinkPic.setVisibility(View.INVISIBLE);

                }

                //先给每个item设置点击事件再添加
                itemLayout.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listListener.onItemClick(drinkInfo);
                    }
                });
                ll.addView(itemLayout);


                index = index+1;
//                ImageView iv = new ImageView(context);
//                iv.setBackgroundResource(R.mipmap.ic_launcher);
//                r.addView(iv);


            }

            this.addView(r);

        }

    }

    public void refView(){

        Log.i(TAG,"drinkInfos:"+drinkInfos.size());
        Log.i(TAG,"myCache:"+myCache.size());
        for(int i = 0 ; i <index;i++){
            ViewHolder holder = myCache.get(drinkInfos.get(i).getNum()+"");
            int count =  DrinkCountUtil.getDrinkCountByNum(context,drinkInfos.get(i).getNum()+"");
            Log.i(TAG, i+": "+String.valueOf(holder==null));
            holder.tvPrice.setText(drinkInfos.get(i).getPrice()+"");
            if(holder.count!=count){

            if(count>0){
                holder.ivDrinkOver.setVisibility(View.INVISIBLE);
                holder.ivDrinkPic.setVisibility(View.VISIBLE);
                GlideUtil.dontAnimLoad(context,drinkInfos.get(i).getMinPicUrl(),holder.ivDrinkPic);
                holder.count = count;
            }else {
                holder.ivDrinkOver.setVisibility(View.VISIBLE);
                holder.ivDrinkPic.setVisibility(View.INVISIBLE);
            }
            }else{
                //未做修改的数据
            }
        }
    }


    class ViewHolder{

        ImageView ivDrinkPic;
        ImageView ivDrinkOver;
        TextView tvPrice;
        int count;
    }





}
